package com.yipinhouse.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipinhouse.backstage.infra.mapper.ShopMapper;
import com.yipinhouse.backstage.infra.model.Shop;
import com.yipinhouse.backstage.service.ShopService;
import com.yipinhouse.backstage.service.dto.AllShopDto;
import com.yipinhouse.backstage.service.dto.ShopDto;
import com.yipinhouse.backstage.service.param.PromotionServiceParam;
import com.yipinhouse.backstage.service.param.ShopServiceParam;
import com.yipinhouse.common.exception.ShopException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-30
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {


    /**
     * 添加门店
     *
     * @param shopServiceParam
     * @return
     */
    @Override
    public ShopDto addShop(ShopServiceParam shopServiceParam) {
        if (shopServiceParam.getLandline().length() != 7 && shopServiceParam.getLandline().length() != 8) {
            throw new ShopException("固定电话只能是7-8位数", 1001);
        }
        Shop address = getOne(new QueryWrapper<Shop>().eq("address", shopServiceParam.getAddress()));
        if (address != null) {
            throw new ShopException("该区域已有门店", 1002);
        }
        Shop shop = new Shop();
        shop.setShopName(shopServiceParam.getShopName());
        shop.setAddress(shopServiceParam.getAddress());
        shop.setLandline(shopServiceParam.getLandline());
        save(shop);
        ShopDto shopDto = BeanUtil.copyProperties(shop, ShopDto.class);
        return shopDto;
    }

    /**
     * 展示所有门店
     *
     * @return
     */
    @Override
    public List<AllShopDto> listShop() {
        List<Shop> list = list();
        List<AllShopDto> allShopDtos = BeanUtil.copyToList(list, AllShopDto.class);
        return allShopDtos;
    }

    /**
     * 查询门店详情
     *
     * @param shopId
     * @return
     */
    @Override
    public ShopDto checkShop(Integer shopId) {
        Shop shop = getById(shopId);
        if (shop == null) {
            throw new ShopException("门店不存在", 1003);
        }
        ShopDto shopDto = BeanUtil.copyProperties(shop, ShopDto.class);
        return shopDto;
    }

    /**
     * 升职，查看门店是否已有店长
     *
     * @param shopId
     */
    @Override
    public void checkShopowner(Integer shopId) {
        Shop shop = getById(shopId);
        if (shop.getBrokerId() != null) {
            throw new ShopException("该门店已有店长", 1004);
        }
    }

    /**
     * 升职，对门店表插入店长信息
     *
     * @param param
     */
    @Override
    public void promotion(PromotionServiceParam param, Integer shopId) {
        Shop shop = getById(shopId);
        shop.setBrokerId(param.getBrokerId());
        shop.setPhone(param.getPhone());
        shop.setNegozianteName(param.getNegozianteName());
        updateById(shop);
    }

}
